# Copyright (c) 2019 Presto Labs Pte. Ltd.
# Author: leon

import logging
from coin.base.book import BookBuilderBase
from coin.exchange.uniswap_v2.kr_rest.product import UniswapProduct
from coin.exchange.base.kr_rest.py_product import PyProduct


class BookEntry(object):
  def __init__(self, price, qty):
    self.price = float(price)
    self.qty = float(qty)


class BookBuilder(BookBuilderBase):
  ProductType = UniswapProduct

  def __init__(self, native_symbol, book_builder_observer=None):
    super().__init__("Uniswap", native_symbol, book_builder_observer=book_builder_observer)

  def bbo(self, data: dict):
    bid0_p, bid0_q = data['bid0']
    self._insert_bid(bid0_p, bid0_q)

    ask0_p, ask0_q = data['ask0']
    self._insert_ask(ask0_p, ask0_q)

  def _insert_bid(self, price, qty):
    assert price > 0
    assert qty > 0
    entry = BookEntry(price, qty)
    self._bid_price_map[price] = entry

  def _insert_ask(self, price, qty):
    assert price > 0
    assert qty > 0
    entry = BookEntry(price, qty)
    self._ask_price_map[price] = entry
